#!/usr/bin/perl 
######################################################################
###
# Proxyfinder 1.1 EN                                                  
#
#                                                                     
#
# Finding proxies and optional pinging them.                          
#
#                                                                     
#
# Arguments:                                                          
#
# --progress-bar=[0|1] Showing progress bar.                          
#
# --ping               Pinging them. Default 0                        
#
# Output format:                                                      
#
# --text     || --cikti=0  Text output. Default.                      
#
# --raw-text || --cikti=1  Raw text output (ip:port).                 
#
# --xml      || --cikti=2  XML output.                                
#
#                                                                     
#
# Example:                                                            
#
#   ./proxyfinder.pl --raw-text --ping                                
#
#                                                                     
#
# Proxyfinder 1.1 EN                                                  
#
# Copyright (C) 2007  Onur Aslan <onuraslan@gmail.com>                
#
#                                                                     
#
# This program is free software: you can redistribute it and/or modify
#
# it under the terms of the GNU General Public License as published by
#
# the Free Software Foundation, either version 3 of the License, or   
#
# any later version.                                                  
#
#                                                                     
#
# This program is distributed in the hope that it will be useful,     
#
# but WITHOUT ANY WARRANTY; without even the implied warranty of      
#
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       
#
# GNU General Public License for more details.                        
#
#                                                                     
#
# You should have received a copy of the GNU General Public License   
#
# along with this program.  If not, see <http://www.gnu.org/licenses/>
#
######################################################################
###

use strict;
use HTML::TokeParser;
use LWP::Simple;
use Net::Ping;

# Ayarlar
my $ping_at = 0;      # Proxyler bulunduktan sonra pinglensin mi?
my $cikti = 0;        # Cikti bicimi:
# Text = 0
# Raw text (ip:port) = 1
# XML = 2
my $progress_bar = 1; # Progress bar olsun mu olmasin mi
my $quiet = 0;        # Herhangi bir cikti gosterilmeden calissin mi?

# Bundan sonrasina karismaniza gerek yok

my $proxyler;
my $out;
my $progress;
local $| = 1;
$progress_bar = 0 if !eval "require Term::ProgressBar;" || $quiet == 1;
# Eger XML::Simple modulu bulunamazsa devam etmesi icin eval kullanild

eval "use XML::Simple;";

sub proxybul
{
    my $sayfa = "01";
    my @raw_proxyler;
    my @raw_proxyler2;
    foreach (1..30)
    {
	my $url = "http://www.samair.ru/proxy/type-$sayfa.htm";
	my $icerik = get ($url);
# Eger bir sayfa acilamazsa digerine geciyor
	if (!$icerik)
	{
	    $progress->update () if $progress_bar;
	    next;
	}
	my $html = HTML::TokeParser->new (\$icerik)
	    or die "File doesn't open: $!\n";
	while (my $icerik = $html->get_tag ("td"))
	{
	    my $text  = $html->get_trimmed_text ("/td");
	    $text =~ s/: /:/;
	    if ($text !~ /IP address/
		    && $text !~ /Anonymity level/
		    && $text !~ /Checked time/
		    && $text !~ /Country/)
	    {
		push @raw_proxyler, $text;
	    }
	}
	$sayfa++;
	$progress->update () if $progress_bar;
    }
# Simdi proxyler cikariliyor
    print "\n" if $progress_bar && $ping_at;
    progressbar_yarat ("Pinging proxies", (($#raw_proxyler-1)/4)-1) if $ping_at;
    for (my $i = 0; $i < @raw_proxyler; $i = $i+4)
    {
	my $durum;
	my $ping_return;
	my $ping_duration;
	my ($proxy, $port) = $raw_proxyler[$i] =~ /(.*):(.*)/;
	if ($ping_at)
	{
	    my $ping = Net::Ping->new ();
	    $ping->hires ();
	    ($ping_return, $ping_duration, $proxy) = $ping->ping ($proxy, 2);
	    if ($ping_return)
	    {
		$durum = "Online";
		$ping_duration = 1000 * $ping_duration;
		($ping_duration) = $ping_duration =~ /([\d]*)\./;
	    }
	    else
	    {
		$durum = "Offline";
		$ping_duration = 0;
	    }
	    $ping->close;
	    $progress->update () if $progress_bar;
	}
	else
	{
	    $durum = "Didn't pinged";
	    $ping_duration = 0;
	}
	push @raw_proxyler2, {
	    "ip" => $proxy,
		"port" => $port,
		"type" => $raw_proxyler[$i+1],
		"country" => $raw_proxyler[$i+3],
		"status" => $durum,
		"ping" => $ping_duration,
		"checktime" => time
	}
    }
    $proxyler = { "complete_time" => time, "proxy" => [@raw_proxyler2] };
}

sub xml
{
    $out = XMLout ($proxyler, KeyAttr => 'ip', XMLDecl => "<?xml version='1.0'?>")
}

sub raw_text
{
    foreach (@{$proxyler->{proxy}})
    {
	print $_->{ip}, ":", $_->{port}, "\n";
    }
}

sub text
{
    print "IP\t\t\tPort\t\tCountry\t\tStatus\t\tPing\n";
    foreach (@{$proxyler->{proxy}})
    {
	print $_->{ip}, "\t\t", $_->{port}, "\t\t", $_->{ulke}, "\t\t", $_->{durum},, "\t\t", $_->{ping}, "\n";
    }
}

sub conf_parse
{
    $_ = join (" ", @ARGV);
    if (/--version/)
    {
	print "Proxyfinder 1.0\nCopyrigt 2007 Onur Aslan - Licensend und
	    +er GPLv3\n";
	exit 0;
    }
    $ping_at = 1 if /--ping/;
    $cikti = $1 if /--cikti=([0|1|2])/;
    $cikti = 0 if /--text/;
    $cikti = 1 if /--raw-text/;
    $cikti = 2 if /--xml/;
    $progress_bar = $1 if /--progress-bar([0|1])/;
    $quiet = 1 && $progress_bar = 0 if /-q/ || /--quiet/;

}

sub progressbar_yarat
{
    if ($progress_bar)
    {
	$progress = Term::ProgressBar->new ({name => $_[0], count => $_[1], ETA => "linear"});
	$progress->max_update_rate (1);
    }
    else
    {
	print $_[0]."\n";
    }
}

sub goster
{
    text if $cikti == 0;
    raw_text if $cikti == 1;
    xml if $cikti == 2;
    print $out if !$quiet;
}


conf_parse;
progressbar_yarat "Getting proxies", 30;
proxybul;
goster;
0;
